package cnam.smb215.findfriends;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SearchActivity extends Activity {
	
	LinearLayout mLinearLayoutNames;
	EditText mEditTextSearch;
	
	NamesUpdateAsyncTask mAsyncTask;
	
	ArrayList<String> mListNames;
	ArrayList<TextView> mListTextViews;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);
		
		mLinearLayoutNames = (LinearLayout)findViewById(R.id.linearLayoutSearchNames);
		mEditTextSearch = (EditText)findViewById(R.id.editTextSearchText);
		mListNames = getIntent().getStringArrayListExtra(Globals.EXTRA_NAMES);
		
		mEditTextSearch.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {}
			
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			
			public void afterTextChanged(Editable s) {
				if(mAsyncTask != null && mAsyncTask.getStatus() == Status.RUNNING) {
					mAsyncTask.cancel(true);
				}
				mAsyncTask = new NamesUpdateAsyncTask();
				mAsyncTask.execute(s.toString());
			}
		});
		
		mListTextViews = new ArrayList<TextView>();
		
		for(int i=0;i<mListNames.size();i++) {
			TextView textView = new TextView(this);
			textView.setText(mListNames.get(i));
			textView.setTag(Integer.valueOf(i));
			textView.setTextSize(22f);
			textView.setOnClickListener(mOnClickListener);
			
			mListTextViews.add(textView);
			mLinearLayoutNames.addView(textView);
		}
	}
	
	private OnClickListener mOnClickListener = new OnClickListener() {
		public void onClick(View v) {
			TextView textView = (TextView)v;
			Integer index = (Integer)textView.getTag();
			
			Intent returnIntent = new Intent();
			returnIntent.putExtra(Globals.EXTRA_RESULT, index);
			setResult(RESULT_OK, returnIntent);     
			finish();
		}
	};
	
	private class NamesUpdateAsyncTask extends AsyncTask<String, Object, Void> {
		
		@Override
		protected Void doInBackground(String... params) {
			String query = params[0];
			
			for(int i=0;i<mListNames.size();i++) {
				if(isCancelled()) {
					break;
				}
				if(query == null || query.isEmpty()) {
					publishProgress(Integer.valueOf(i), Boolean.TRUE);
				}
				else {
					publishProgress(Integer.valueOf(i), Boolean.valueOf(mListNames.get(i).toUpperCase().contains(query.toUpperCase())));
				}
			}
			return null;
		}
		
		@Override
		protected void onProgressUpdate(Object... values) {
			int index = (Integer)values[0];
			boolean stat = (Boolean)values[1];
			
			mListTextViews.get(index).setVisibility(stat? View.VISIBLE:View.GONE);
		}
	}
}
